package SecondeStage.IO.In_OutputStream;

import java.io.*;

public class FileOutputStream_ {
    public static void main(String[] args) {
        FileOutputStream_ fileOutputStream = new FileOutputStream_();
        fileOutputStream.writeFile();

    }

    public void writeFile(){
        String filePath ="D:\\Java\\JavaProject\\a.txt";
        FileOutputStream fileOutputStream = null;
        File file =new File(filePath);
        try {
            file.createNewFile();
            // fileOutputStream = new FileOutputStream(filePath); 创建方式，写入内容时，会覆盖原来的内容
            // fileOutputStream = new FileOutputStream(filePath,true); 创建方式，写入内容时，会追加到文件后面
            fileOutputStream = new FileOutputStream(filePath,true);
            // 写入一个字节
            fileOutputStream.write('H');
            //写入字符串
            String str = "hello,world!";
            fileOutputStream.write(str.getBytes());
            fileOutputStream.write(str.getBytes(),0,3);

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
